$(document).ready(function(){
	var firstname = $("#firstname");
	var lastname = $("#lastname");
	var username = $("#username");
	var email = $("#email");
	var pass1 = $("#pass");
	var pass2 = $("#confirmpass");
	var secquestion = $("#securityquestion");
	var secanswer = $("#secanswer");

	firstname.blur(vFirstname);
	lastname.blur(vLastname);	
	username.blur(vUserName);
	email.blur(vUserEmail);
	pass1.blur(vUserPass1);
	pass2.blur(vUserPass2);
	secquestion.blur(vSecQuestion);
	secanswer.blur(vSecAnswer);
	
	
	function vFirstname(){
		if(firstname.val().length < 2){
			firstname.removeClass("text_field");
			firstname.addClass("text_field_error");
			return false;			
		}
		else{
			firstname.addClass("text_field");
			firstname.removeClass("text_field_error");
			return true;		
		}			
	}
	function vLastname(){
		if(lastname.val().length < 2){
			lastname.removeClass("text_field");
			lastname.addClass("text_field_error");
			return false;			
		}
		else{
			lastname.addClass("text_field");
			lastname.removeClass("text_field_error");
			return true;		
		}			
	}
	function vUserName(){
		if(username.val().length < 2){
			username.removeClass("text_field");
			username.addClass("text_field_error");
			return false;
		}
		else{
			username.addClass("text_field");
			username.removeClass("text_field_error");
			return true;			
		}
	}
	
	function vUserEmail(){
		var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;

		if(emailPattern.test(email.val())){
			email.addClass("text_field");
			email.removeClass("text_field_error");
			return true;
		}
		else{
			email.removeClass("text_field");
			email.addClass("text_field_error");
			return false;
		}	
		
	}

	function vUserPass1(){
		if(pass1.val().length < 5){
			pass1.removeClass("text_field");
			pass1.addClass("text_field_error");
			return false;
		}
		else{
			pass1.addClass("text_field");
			pass1.removeClass("text_field_error");
			return true;			
		}
	}
	
	function vUserPass2(){
		if(pass1.val() != pass2.val()){
			pass2.removeClass("text_field");
			pass2.addClass("text_field_error");
			return false;			
		}
		else{
			pass2.addClass("text_field");
			pass2.removeClass("text_field_error");
			return true;		
		}	
	}
	
	function vSecQuestion(){
		if(!(secquestion.val())){
			secquestion.addClass("error");
			return false;			
		}
		else{
			secquestion.removeClass("error");
			return true;			
		}
	}
	
	function vSecAnswer(){
		if(secanswer.val() == ""){
			secanswer.removeClass("text_field");
			secanswer.addClass("text_field_error");
			return false;			
		}
		else{
			secanswer.addClass("text_field");
			secanswer.removeClass("text_field_error");
			return true;		
		}			
	}

	
	$("#signupForm").submit(function(){
		if (vUserName() & vUserEmail() &  vUserPass1() & vUserPass2() & vSecQuestion() & vSecAnswer() & vFirstname() & vLastname() ) {
			return true;
		}
		else {
			return false;
		}
	});

	
});


